import { localCache } from '@/utils/storage'
import { createRouter, createWebHashHistory } from 'vue-router'
import { LOGIN_TOKEN } from '@/global/constans'
const router = createRouter({
  history: createWebHashHistory(),
  //配置映射关系
  routes: [
    {
      path: '/',
      redirect: '/Main'
    },
    {
      path: '/Login',
      component: () => import('../views/login/Login.vue')
    },
    {
      path: '/Main',
      component: () => import('../views/main/Main.vue'),
      children: [
        //1-1系统总览-核心技术
        {
          path: '/analysis/overView',
          component: () => import('../views/analysis/overView.vue')
        },
        //1-2系统总览-商品统计
        {
          path: '/analysis/dashBoard',
          component: () => import('../views/analysis/dashBoard.vue')
        },
        //2-1系统管理-角色管理
        {
          path: '/systemManagement/roleManagement',
          component: () => import('../views/systemManagement/roleManagement.vue')
        },
        //2-2系统管理-用户管理
        {
          path: '/systemManagement/userMangement',
          component: () => import('../views/systemManagement/userMangement.vue')
        },
        //2-3系统管理-菜单管理
        {
          path: '/systemManagement/menuManagement',
          component: () => import('../views/systemManagement/menuManagement.vue')
        },
        //2-4系统管理-部门管理
        {
          path: '/systemManagement/deptMangement',
          component: () => import('../views/systemManagement/deptMangement.vue')
        },
        //3-1商品中心-商品类别
        {
          path: '/goodsCenter/goodsCategory',
          component: () => import('../views/goodsCenter/goodsCategory.vue')
        },
        //3-2商品中心-商品信息
        {
          path: '/goodsCenter/goodsInfo',
          component: () => import('../views/goodsCenter/goodsInfo.vue')
        }
      ]
    },
    {
      path: '/:pathMatch(.*)',
      component: () => import('../views/not-found/NotFound.vue')
    }
  ]
})

//路由导航守卫
router.beforeEach((to, from) => {
  const token = localCache.getStorage(LOGIN_TOKEN)
  if (to.path == '/main' && !token) {
    //只有登录成功(判断有没有token，才能进入Main
    return '/Login'
  }
  if (to.path == '/main') {
    return '/analysis/overView'
  }
})

export default router
